/* eslint-disable curly */
/* eslint-disable nonblock-statement-body-position */
/*
 * @Description: 工具函数
 * @Author: 0001100986/LiuShuai
 * @Date: 2021-06-22 14:09:49
 * @LastEditTime: 2021-07-16 09:39:20
 * @LastEditors: 0001100986/LiuShuai
 */
import { Message } from 'element-ui';
import setting from '@/config/setting';
import CryptoJS from 'crypto-js';

// 上传文件校验
export function uploadFileCheck (file, optionFiles, imageMaxSize) {
  let fileSize = file.size / 1024; // kb
  let imgList = ['.jpg', '.png', '.jpeg'];
  let type = file.name.substring(file.name.lastIndexOf('.'));

  if (optionFiles.indexOf(type) === -1) {
    Message.error('文件格式不符合文件上传规范！');
    return false;
  }

  // 图片大小限制
  if (imgList.indexOf(type) !== -1 && fileSize > imageMaxSize) {
    // eslint-disable-next-line radix
    Message.error(`图片容量超过最大限制，当前图片${parseInt(fileSize)}kb，最大${imageMaxSize}kb`);
    return false;
  }
}

// 滚动到某个地方
export function toScrollTop (elementDom) {
  document.querySelector(elementDom).scrollIntoView(true);
}

// 全屏
export function fullScreen () {
  let el = document.documentElement;
  let rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen;

  if (rfs) {
    rfs.call(el);
  } else if (typeof window.ActiveXObject !== 'undefined') {
    // for IE，这里其实就是模拟了按下键盘的F11，使浏览器全屏
    // eslint-disable-next-line no-undef
    let wscript = new ActiveXObject('WScript.Shell');
    if (wscript != null) {
      wscript.SendKeys('{F11}');
    }
  }
}

// 退出全屏
export function exitScreen () {
  let el = document;
  let cfs = el.cancelFullScreen || el.webkitCancelFullScreen || el.mozCancelFullScreen || el.exitFullScreen;

  if (cfs) {
    cfs.call(el);
  }
  else if (typeof window.ActiveXObject !== 'undefined') {
    // for IE，这里和fullScreen相同，模拟按下F11键退出全屏
    // eslint-disable-next-line no-undef
    let wscript = new ActiveXObject('WScript.Shell');
    if (wscript != null) {
      wscript.SendKeys('{F11}');
    }
  }
}

// 加密方法
export function encrypt (word) {
  let key = CryptoJS.enc.Utf8.parse(setting.cryptojs.key);
  let iv = CryptoJS.enc.Utf8.parse(setting.cryptojs.iv);

  let srcs = CryptoJS.enc.Utf8.parse(word);
  let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
  return encrypted.ciphertext.toString().toUpperCase();
}

// 解密方法
export function decrypt (word) {
  let key = CryptoJS.enc.Utf8.parse(setting.cryptojs.key);
  let iv = CryptoJS.enc.Utf8.parse(setting.cryptojs.iv);

  let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
  let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
  let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
  let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
  return decryptedStr.toString();
}

// 检测字符串是否为Base64
function isBase64 (str) {
  try {
    return btoa(atob(str)) === str;
  } catch (err) {
    return false;
  }
}

// 解码base64
// eslint-disable-next-line complexity
export function emailBase64Decode (str) {
  if (str && isBase64(str)) {
    let hexOut = false;
    // eslint-disable-next-line no-array-constructor
    let base64DecodeChars = new Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
      52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
      -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
      15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
      -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
      41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
    // eslint-disable-next-line one-var, one-var-declaration-per-line
    let c1, c2, c3, c4;
    // eslint-disable-next-line one-var, one-var-declaration-per-line
    let i, len, out;
    let charCode;

    len = str.length;
    i = 0;
    out = hexOut ? [] : '';
    while (i < len) {
      /* c1 */
      do {
        c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
      } while (i < len && c1 === -1);
      if (c1 === -1)
        break;

      /* c2 */
      do {
        c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
      } while (i < len && c2 === -1);
      if (c2 === -1)
        break;

      charCode = (c1 << 2) | ((c2 & 0x30) >> 4);
      hexOut ? out.push(charCode) : out += String.fromCharCode(charCode);

      /* c3 */
      do {
        c3 = str.charCodeAt(i++) & 0xff;
        if (c3 === 61)
          return out;
        c3 = base64DecodeChars[c3];
      } while (i < len && c3 === -1);
      if (c3 === -1)
        break;
      charCode = ((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2);
      hexOut ? out.push(charCode) : out += String.fromCharCode(charCode);

      /* c4 */
      do {
        c4 = str.charCodeAt(i++) & 0xff;
        if (c4 === 61)
          return out;
        c4 = base64DecodeChars[c4];
      } while (i < len && c4 === -1);
      if (c4 === -1)
        break;
      charCode = ((c3 & 0x03) << 6) | c4;
      hexOut ? out.push(charCode) : out += String.fromCharCode(charCode);
    }
    return out;
  } else {
    return str;
  }
}
